//
//  CameraManager.m
//  DDKelai
//
//  Created by 宋华 on 16/4/18.
//  Copyright © 2016年 Admin. All rights reserved.
//

#import "CameraManager.h"

@implementation CameraManager

+ (CameraManager *)shareInstance {
    static CameraManager *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

+ (ALAuthorizationStatus)authorizationStatusForPhotoLibary {
    ALAuthorizationStatus authorStatus = [ALAssetsLibrary authorizationStatus];
    if (authorStatus == ALAuthorizationStatusRestricted || authorStatus == ALAuthorizationStatusDenied) {
        NSString*prodName =[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
        NSString *tips = [NSString stringWithFormat:@"请在iPhone的”设置-%@-照片“选项中，允许%@访问你的手机相册",prodName,prodName];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"未获得授权使用相册" message:tips delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alertView.tag = 101;
        [alertView show];
    }
    return authorStatus;
}

+ (AVAuthorizationStatus)authorizationStatusForCamera:(void (^)(BOOL granted))requestDoneBlock {
    
    //判断相机是否能够使用
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  if(status == AVAuthorizationStatusDenied || status == AVAuthorizationStatusRestricted){
      NSString*prodName =[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
      NSString *tips = [NSString stringWithFormat:@"请在iPhone的”设置-%@-相机“选项中，允许%@访问你的手机相机",prodName,prodName];
      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"未获得授权使用摄像头" message:tips delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
      [alertView show];
      alertView.tag = 100;
  } else if(status == AVAuthorizationStatusNotDetermined){
      if (requestDoneBlock) {
          requestDoneBlock(YES);
      }
  } else if(status == AVAuthorizationStatusAuthorized){
        if (requestDoneBlock) {
            requestDoneBlock(YES);
        }
    }
    return status;
}

#pragma mark delegate : UIAlertViewDelegate
+ (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 100 && buttonIndex == 0) {
        NSString *path = [NSString stringWithFormat:@"prefs:root=%@",[[NSBundle mainBundle] bundleIdentifier]];
        NSURL *url = [NSURL URLWithString:path];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
        }
    } else  if (alertView.tag == 101 && buttonIndex == 0) {
        NSString *path = [NSString stringWithFormat:@"prefs:root=%@",[[NSBundle mainBundle] bundleIdentifier]];
        NSURL *url = [NSURL URLWithString:path];
        if ([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
        }
    }
  
}

@end
